//
//  ASResearchTypeVC.m
//  AttendenceSys
//
//  Created by 窦芳俊 on 16/4/13.
//  Copyright © 2016年 窦芳俊. All rights reserved.
//

#import "ASResearchTypeVC.h"
#import "ASResearchVC.h"
#import "ASResearchTypeInfo.h"

#define kResearchTypeCellHeight  50

@interface ASResearchTypeVC () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView        *rTypeTableView;
@property (nonatomic, strong) NSMutableArray     *researchArray;

@end

@implementation ASResearchTypeVC

- (instancetype)initWithAchiTypeId:(NSInteger)typeId typeName:(NSString *)typeName
{
    self = [super init];
    if (self)
    {
        _achiTypeId = typeId;
        _achiTypeName = typeName;
    }
    
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //_researchArray = @[@"论文", @"专著", @"项目", @"专利", @"其它"];
    _researchArray = [[NSMutableArray alloc] initWithCapacity:1];
    
    self.strNavTitle = @"科研类型";
    self.view.backgroundColor = [UIColor whiteColor];
    
    _rTypeTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _rTypeTableView.frame = CGRectMake(0, 0, self.view.frame.size.width, 250);
    _rTypeTableView.delegate = self;
    _rTypeTableView.dataSource = self;
    _rTypeTableView.backgroundColor = [UIColor whiteColor];
    _rTypeTableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    _rTypeTableView.rowHeight = kResearchTypeCellHeight;
    [self.view addSubview:_rTypeTableView];
    
    //[_rTypeTableView reloadData];
    [self loadType];
}

- (void)loadType
{
    //http://211.65.216.54/researchSubject.action
    
    if (_researchArray == nil)
    {
        _researchArray = [[NSMutableArray alloc] init];
    }
    
    NSString *refer = @"researchSubject.action";
    NSString *prefix = kASHttpUrl(refer);
    
    [SVProgressHUD showWithStatus:@"获取中..." maskType:SVProgressHUDMaskTypeClear];
    ASNetworkEngine *networkingEngine = [ASNetworkEngine sharedInstance];
    
    typeof(self) __weak weakSelf = self;
    [networkingEngine POST:prefix parameters:nil success:^(id responseObject) {
        NSDictionary* jsonRsp = (NSDictionary *)responseObject;
        if (isSuccessRsp(jsonRsp))
        {
            NSArray* data = [jsonRsp objectForKey:kASJsonData];
            [weakSelf parseType:data];
            [SVProgressHUD dismiss];
            
            [_rTypeTableView reloadData];
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:errorMsgRsp(jsonRsp)];
        }
    } failure:^(NSError *error) {
        //[SVProgressHUD showErrorWithStatus:error.description];
        [SVProgressHUD showErrorWithStatus:@"网络异常，请稍后再试"];
    }];
}

- (void)parseType:(NSArray *)array
{
    for (int i = 0; i < [array count]; i++)
    {
        ASResearchTypeInfo* type = [[ASResearchTypeInfo alloc] init];
        type.researchSubjectId = [[array objectAtIndex:i] objectForKey:@"researchSubjectId"];
        type.researchSubjectName = [[array objectAtIndex:i] objectForKey:@"researchSubjectName"];
        
        [_researchArray addObject:type];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_researchArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"researchTypeTableViewCellIdentifier"];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] init];
    }
    
    ASResearchTypeInfo *type = [_researchArray objectAtIndex:[indexPath row]];
    cell.textLabel.text = type.researchSubjectName;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
//    if (self.delegate && [self.delegate respondsToSelector:@selector(selectResearchType:)])
//    {
//        [self.delegate selectResearchType:[_researchArray objectAtIndex:[indexPath row]]];
//    }
    
    // 弹入指定科研类型页面
    ASResearchVC* researchVC = [[ASResearchVC alloc] initWithAchiTypeId:_achiTypeId
                                                               typeName:_achiTypeName
                                                           researchType:[_researchArray objectAtIndex:[indexPath row]]];
    researchVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:researchVC animated:YES];
}



@end
